package com.huangyi.adminservice.house.service.mq;


import com.huangyi.adminapi.appuser.domain.dto.AppUserDTO;
import com.huangyi.adminservice.house.service.IHouseService;
import com.huangyi.adminservice.user.controller.RabbitConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
@RabbitListener(bindings = {
        @QueueBinding(
                value = @Queue(),
                exchange = @Exchange(value = RabbitConfig.EXCHANGE_NAME, type = ExchangeTypes.FANOUT)
        )
})
public class EditAppUserMessageReceiver {
        @Autowired
        private IHouseService houseService;

        @RabbitHandler
        public void process(AppUserDTO appUserDTO) {
                log.info("MQ成成功接收到用户的修改信息");
                if(appUserDTO == null || appUserDTO.getUserId() == null) {
                        log.error("用户修改信息为空或修改信息的用户ID为空");
                        return;
                }

                try{
                        // 根据用户id, 获取用户下的所有房源 id 列表
                        List<Long> houseIds = houseService.listByUserId(appUserDTO.getUserId());
                        for(Long houseId : houseIds) {
                                houseService.cacheHouse(houseId);
                        }

                }catch (Exception e){
                        log.warn("处理用户更新时, 更新房源相关信息异常 appUserDTO:{}", appUserDTO, e);
                }


        }
}
